/*************************************************************************
> File Name: construction.cpp
> Author: icebear
> Mail: 3322366195@qq.com
> Created Time: 2023-08-01 14:42:34
> Description: Z 字变换
    vector转向！！！
************************************************************************/

#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
    string convert(string s, int numRows)
    {
        if (numRows == 1)
            return s;
        vector<string> rows(numRows); // 字符队列的集合

        // 行转向标志，极妙
        int flag = 1;
        // 行下标索引
        int idxRows = 0;
        for (int i = 0; i < s.size(); i++)
        {
            rows[idxRows].push_back(s[i]);
            // 行下标 z字移动
            idxRows += flag;

            if (idxRows == numRows - 1 || idxRows == 0)
            {
                // 转向
                flag = -flag;
            }
        }
        string res;
        for (auto row : rows)
        {
            // 拿到答案
            res += row;
        }
        return res;
    }
};

int main(int argc, char const *argv[])
{
    Solution solution;
    string s = "PAYPALISHIRING";
    int numRows = 3;
    string result = solution.convert(s, numRows);
    cout << result << endl;
    return 0;
}
